using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace Havivi.Scrum.Features.Havivi.Scrum
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("16b1951e-386e-4006-b49f-aff911fc97f0")]
    public class HaviviEventReceiver : SPFeatureReceiver
    {
       
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            if (properties.Feature.Parent is SPSite)
            {

                SPSite site = (SPSite)properties.Feature.Parent;
                using (SPWeb web = site.OpenWeb())
                {
                    if (!ListExists(web,Constants.SprintBacklog ))
                    {
                        Guid guid = web.Lists.Add(Constants.SprintBacklog,
                                               "",
                                               SPListTemplateType.Tasks);
                        SPList list = web.Lists[Constants.SprintBacklog];
                        string id = list.Fields.Add(Constants.ScrumTaskBoardColumn, SPFieldType.Text, false);
                        SPField field = list.Fields[id];
                       
                        field.DefaultValue = "0";
                        if (field.CanToggleHidden)
                        {
                            field.Hidden = true;
                        }
                        field.Update();
                        id = list.Fields.Add(Constants.ScrumBoardWebpartID, SPFieldType.Text, false);
                        field = list.Fields[id];

                        if (field.CanToggleHidden)
                        {
                            field.Hidden = true;
                        }
                        field.Update();
                        list.OnQuickLaunch = false;
                        web.AllowUnsafeUpdates = true;
                        list.Update();
                        web.Update();
                    }
                    if (!ListExists(web, Constants.SprintBacklog + " Columns"))
                    {
                        Guid guid = web.Lists.Add(Constants.SprintBacklog + " Columns",
                                               "",
                                               SPListTemplateType.GenericList);
                        SPList list = web.Lists[Constants.SprintBacklog + " Columns"];
                        list.OnQuickLaunch = false;
                        web.AllowUnsafeUpdates = true;
                        list.Update();
                        web.Update();
                    }

                    try
                    {
                        SPList listColumns = web.Lists[Constants.SprintBacklog + " Columns"];
                        if (listColumns.ItemCount == 0)
                        {
                            SPListItem item = listColumns.Items.Add();
                            item["Title"] = "To Do";
                            item.Update();
                            item = listColumns.Items.Add();
                            item["Title"] = "In Process";
                            item.Update();
                            item = listColumns.Items.Add();
                            item["Title"] = "To Verify";
                            item.Update();
                            item = listColumns.Items.Add();
                            item["Title"] = "Done";
                            item.Update();
                        }
                    }
                    catch
                    {
                        //error in demo data - do nothing
                    }

                }
            }

        }

        public static bool ListExists(SPWeb web, string listName)
        {
            return web.Lists.Cast<SPList>().Any(list => string.Equals(list.Title, listName));
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {


        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        { }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}
